package cn.com.wangluotongxin.service.service.impl;

import cn.com.wangluotongxin.admin.model.AdminSmsPush;
import cn.com.wangluotongxin.admin.model.constants.SmsAppInfo;
import cn.com.wangluotongxin.admin.model.constants.YesNo;
import cn.com.wangluotongxin.common.eureka.constants.SmsPushContent;
import cn.com.wangluotongxin.common.eureka.constants.SmsTemplate;
import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.pojo.SysParamsInfo;
import cn.com.wangluotongxin.redis.cache.SysParamsCacheService;
import cn.com.wangluotongxin.service.mapper.AdminSmsPushMapper;
import cn.com.wangluotongxin.service.service.IAdminSmsPushService;
import cn.com.wangluotongxin.service.utils.TengXunUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 网络通信-管理端-短信发送 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-01-15
 */
@Service
public class AdminSmsPushServiceImpl extends ServiceImpl<AdminSmsPushMapper, AdminSmsPush> implements IAdminSmsPushService {

    @Autowired
    private SysParamsCacheService paramsCacheService;

    @Override
    public void addCodeSms(SmsAppInfo appInfo, SmsTemplate smsTemplate, String phone, Integer expireTime, String code) {
        SmsPushContent pushContent = new SmsPushContent();
        pushContent.setCode(code);
        pushContent.setPhone(phone);
        pushContent.setSmsSign(smsTemplate.getSmsSign());
        pushContent.setExpireTime(smsTemplate.getExpireTime());
        pushContent.setTemplateId(smsTemplate.getTemplateId());
        pushContent.setContext(smsTemplate.getContext().replace("%code%", code).replace("%expireTime%", String.valueOf(expireTime)));
        this.addSms(appInfo, pushContent);
    }

    @Override
    public void addSms(SmsAppInfo appInfo, SmsPushContent pushContent) {
        AdminSmsPush adminSmsPush = new AdminSmsPush();
        adminSmsPush.setExpireTime(DateUtil.offset(DateUtil.date(), DateField.MINUTE, pushContent.getExpireTime()));
        adminSmsPush.setSendResult(YesNo.YES.getCode());
        adminSmsPush.setSmsAppInfo(appInfo.getCode());
        adminSmsPush.setSmsContent(JSONObject.toJSONString(pushContent));
        adminSmsPush.setCreateTime(DateUtil.date());
        this.save(adminSmsPush);

        // 直接发送
        this.doSmsPush(adminSmsPush);
    }

    @Override
    public void sendSmsError(Integer id, String failInfo) {
        this.update(new LambdaUpdateWrapper<AdminSmsPush>()
                .set(AdminSmsPush::getSendResult, YesNo.NO.getCode())
                .set(AdminSmsPush::getFailInfo, failInfo)
                .eq(AdminSmsPush::getId, id));
    }

    private void doSmsPush(AdminSmsPush adminSmsPush) {
        SysParamsInfo info = paramsCacheService.getParams();
        if(info == null){
            throw new BadResponseException("缺少系统参数配置信息");
        }
        if(StrUtil.isBlank(info.getTxApiAppId()) || StrUtil.isBlank(info.getTxApiSecretId()) || StrUtil.isBlank(info.getTxApiSecretKey())){
            throw new BadResponseException("缺少腾讯SDK API访问参数配置信息");
        }
        SmsPushContent pushContent = JSONObject.parseObject(adminSmsPush.getSmsContent(), SmsPushContent.class);
        try {
            TengXunUtil.sendSms(
                    info.getTxApiSecretId(),
                    info.getTxApiSecretKey(),
                    info.getTxApiAppId(),
                    pushContent.getTemplateId(),
                    pushContent.getSmsSign(),
                    pushContent.getPhone(),
                    pushContent.getContext());
        }catch (Exception ex){
            this.sendSmsError(adminSmsPush.getId(), ex.getMessage());
            throw new BadResponseException(ex.getMessage());
        }
    }
}
